package com.ruoyi.common.utils;

import java.util.Date;

/**
 * cron工具类
 *
 * @author yr
 */
public class CronUtil {

    public static String toCronExpression(MyCalendar calendar,int num, String timeUnit, Date startTime) throws IllegalAccessException {
        String cronExpr;
        switch (timeUnit) {
            case "1":   //秒
                cronExpr = String.format("*/%s * * * * ?", num);
                break;
            case "2":   //分钟
                cronExpr = String.format("%s */%s * * * ?",
                        calendar.getSecond(),
                        num);
                break;
            case "3":   //小时
                cronExpr = String.format("%s %s */%s * * ?",
                        calendar.getSecond(),
                        calendar.getMinute(),
                        num);
                break;
            case "4":   //天
                cronExpr = String.format("%s %s %s */%s * ?",
                        calendar.getSecond(),
                        calendar.getMinute(),
                        calendar.getHour(),
                        num);
                break;
            case "5":   //周
                cronExpr = String.format("%s %s %s */%s * ?",
                        calendar.getSecond(),
                        calendar.getMinute(),
                        calendar.getHour(),
                        num * 7);
                break;
            case "6":   //月
                if (DateUtils.lastDayOfMonth(startTime)) {
                    cronExpr = String.format("%s %s %s L */%s ?",
                            calendar.getSecond(),
                            calendar.getMinute(),
                            calendar.getHour(),
                            num);
                } else {
                    cronExpr = String.format("%s %s %s %s */%s ?",
                            calendar.getSecond(),
                            calendar.getMinute(),
                            calendar.getHour(),
                            calendar.getDay(),
                            num);
                }
                break;
            case "7":   //年
                cronExpr = String.format("%s %s %s %s %s ?",
                        calendar.getSecond(),
                        calendar.getMinute(),
                        calendar.getHour(),
                        calendar.getDay(),
                        calendar.getMonth());
                break;
            default:
                throw new IllegalAccessException("不支持的时间单位!");
        }
        return cronExpr;
    }

    public static String toCronExpressionAppoint(MyCalendar calendar,int num, String timeUnit, Date startTime) throws IllegalAccessException {
        String cronExpr;
        switch (timeUnit) {
            case "1":   //秒
                cronExpr = String.format("%s/%s * * * * ?",
                        calendar.getSecond(),
                        num);
                break;
            case "2":   //分钟
                cronExpr = String.format("%s %s/%s * * * ?",
                        calendar.getSecond(),
                        calendar.getMinute(),
                        num);
                break;
            case "3":   //小时
                cronExpr = String.format("%s %s %s/%s * * ?",
                        calendar.getSecond(),
                        calendar.getMinute(),
                        calendar.getHour(),
                        num);
                break;
            case "4":   //天
                cronExpr = String.format("%s %s %s %s/%s * ?",
                        calendar.getSecond(),
                        calendar.getMinute(),
                        calendar.getHour(),
                        calendar.getDay(),
                        num);
                break;
            case "5":   //周
                cronExpr = String.format("%s %s %s %s/%s * ?",
                        calendar.getSecond(),
                        calendar.getMinute(),
                        calendar.getHour(),
                        calendar.getDay(),
                        num * 7);
                break;
            case "6":   //月
                if (DateUtils.lastDayOfMonth(startTime)) {
                    cronExpr = String.format("%s %s %s L %s/%s ?",
                            calendar.getSecond(),
                            calendar.getMinute(),
                            calendar.getHour(),
                            calendar.getMonth(),
                            num);
                } else {
                    cronExpr = String.format("%s %s %s %s %s/%s ?",
                            calendar.getSecond(),
                            calendar.getMinute(),
                            calendar.getHour(),
                            calendar.getDay(),
                            calendar.getMonth(),
                            num);
                }
                break;
            case "7":   //年
                cronExpr = String.format("%s %s %s %s %s ?",
                        calendar.getSecond(),
                        calendar.getMinute(),
                        calendar.getHour(),
                        calendar.getDay(),
                        calendar.getMonth());
                break;
            default:
                throw new IllegalAccessException("不支持的时间单位!");
        }
        return cronExpr;
    }

    public static String toCronExpressionDaily(MyCalendar calendar){
        return String.format("%s %s %s * * ?",
                calendar.getSecond(),
                calendar.getMinute(),
                calendar.getHour());
    }

}
